﻿using UnityEngine;
using System.Collections.Generic;

public class EnemyShipSpawner : MonoBehaviour {
	public GameObject prefab; // What prefab will spawn
	public float spawnInterval; // How often the prefab will spawn
	float lastSpawnTime; // Used to spawn every interval
	int spawnCount = 0; // Count how many have spawned
	public Vector3 startRotation; // Rotation, converted to Euler
	Queue<GameObject>[] ships; // Queue the ships as they spawn
	public ObjectPool pool;
	public Positions positions;
	public int rowLength, rowWidth;
	public bool positionShift;
	public int retreatCount;
	bool retreating;
	int initialSpawnCount;
	int totalShipCount;
	public bool spawning;
	void Start () {
		retreating = false;
		lastSpawnTime = Time.time;
		ships = new Queue<GameObject>[rowWidth];
		for(int i = 0; i < rowWidth; i++) {
			ships[i] = new Queue<GameObject> ();
		}
	}

	void Update () {
		if(spawning) {
			if(spawnCount < rowLength && (Time.time - lastSpawnTime) > spawnInterval) {
				for(int j = 0; j < rowWidth; j++) {
					GameObject ship = pool.spawnFromPool();
					if(ship != null) {
						ship.transform.position = transform.position;
						ship.transform.rotation = Quaternion.Euler (startRotation);
						ship.GetComponent<EnemyShipFlight>().positionIndex = spawnCount + (j * rowLength);
						ship.GetComponent<EnemyShipFlight>().target = positions.rows[spawnCount + (j * rowLength)];
						ship.GetComponent<EnemyShipFlight>().rotation = ShipRotation.HOMING;
						ship.GetComponent<EnemyShipFlight>().direction = ShipDirection.HOMING;
						ships[j].Enqueue(ship);
						initialSpawnCount++;
					} else {
						Debug.Log ("ship object pool empty");
					}
				}
				lastSpawnTime = Time.time;
				spawnCount++;
			}

			if(positionShift) {
				for(int i = 0; i < rowWidth; i++) {
					if(ships[i].Count > 0) {
						if(ships[i].Peek() != null) {
							if(ships[i].Peek().GetComponent<EnemyShipFlight>().target != null) {
								if(ships[i].Peek().GetComponent<EnemyShipFlight>().target.GetComponent<Position>().rowPosition == RowPosition.BACK) {
									ships[i].Peek().GetComponent<EnemyShipFlight>().target = positions.rows[ships[i].Peek().GetComponent<EnemyShipFlight>().positionIndex - (rowWidth - ships[i].Count + 2)];
									ships[i].Peek().GetComponent<EnemyShipFlight>().targetPosition = ships[i].Peek().GetComponent<EnemyShipFlight>().target.transform.position;
									ships[i].Peek().GetComponent<EnemyShipFlight>().rotation = ShipRotation.HOMING;
									ships[i].Peek().GetComponent<EnemyShipFlight>().direction = ShipDirection.HOMING;
								}
							}
						} else {
							ships[i].Dequeue();
						}
					}
				}
			}
			totalShipCount = 0;
			for(int i = 0; i < ships.Length; i++) {
				foreach(GameObject ship in ships[i]) {
					if(ship != null) {
						totalShipCount++;
					}
				}
			}
			totalShipCount = totalShipCount;
			//Debug.Log ("initial: " + initialSpawnCount);
			//Debug.Log ("totalShipCount: " + totalShipCount);
			//Debug.Log ("max ships: " + (rowLength * rowWidth));

			if(totalShipCount <= retreatCount && initialSpawnCount == (rowLength * rowWidth)) {
				if(!retreating) {
					// Random retreat direction
					for(int i = 0; i < rowWidth; i++) {
						if(ships[i].Count > 0) {
							foreach(GameObject ship in ships[i]) {
								bool direction = Random.value > 0.5f;
								if(ship != null) {
									ship.GetComponent<EnemyShipFlight>().target = null;
									ship.GetComponent<EnemyShipFlight>().turnSpeed = 0.1f;
									if(direction) {
										ship.GetComponent<EnemyShipFlight>().rotation = ShipRotation.RIGHT;
										ship.GetComponent<EnemyShipFlight>().direction = ShipDirection.FORWARD;
									} else {
										ship.GetComponent<EnemyShipFlight>().rotation = ShipRotation.LEFT;
										ship.GetComponent<EnemyShipFlight>().direction = ShipDirection.FORWARD;
									}
								}
							}
						}
					}
					retreating = true;
				}
			}
		}
	}
}
